PHP mail with multiple attachments and message in HTML format [closed]

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2012-09-18T02:58:01Z Indexed on 2012/09/18 3:37 UTC
Read the original article Hit count: 118

Filed under:

I am new to PHP, so please don't mind if my question is silly. I would to like to make a PHP to send email with numerous attachments and the message of the email will be in HTML format.

<html>
<body>

<form action="mail.php" method="post">
    <table>
        <tr>
            <td><label>Name:</label></td>
            <td><input type="text" name="name" /></td>
        </tr>
        <tr>
        <td><label>Your Email:</label></td>
        <td><input type="text" name="email" /></td>
    </tr>
    <tr>
        <td><label>Attachment:</label></td>
        <td><input type="file" name="Attach" /></td>
    </tr>
    <tr>
        <td><input type="submit" value="Submit" /></td>
    </tr>
    </table>
</form>
<script language="javascript">
$(document).ready(function() {
    $("form").submit(function(){
        $.ajax({
            type: "POST",
            url: 'mail.php',
            dataType: 'json',
            data: {
                name: $('#name').val(),
                email: $('#email').val(),
                Attach: $('#Attach').val(),
            },
            success: function(json){
                $(".error, .success").remove();
                if (json['error']){
                    $("form").after(json['error']);
                }
                if (json['success']){
                    $("form").remove();
                    $(".leftColWrap").append(json['success']);
                }
            }               
        });
        return false;
    });
});
</script>

</body>
</html>

This is my HTML for filing in the information. And below is the mail.php

<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));

header('Content-type: application/json');

$timeout = time()+60*60*24*30;
setcookie(Form, date("F jS - g:i a"), $timeout);
$name=$_POST['name'];
$email=$_POST['email'];

$to="[email protected]";

//*** Uniqid Session ***//
$Sid = md5(uniqid(time()));

$headers = "";
$headers .= "From: $email  \n";

$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$Sid."\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n";

$headers .= "--".$Sid."\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";

//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
    $FilesName = $_FILES["fileAttach"]["name"];
$Content = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 
$headers .= "--".$Sid."\n";
$headers .= "Content-Type: application/octet-eam; name=\"".$FilesName."\"\n"; 
$headers .= "Content-Transfer-Encoding: base64\n";
$headers .= "Content-Disposition: attachment; filename=\"".$FilesName."\"\n\n";
$headers .= $Content."\n\n";
}

$message_to="
<html><body>
<table class='page-head' align='center' width='100%'>
<tr>
    <td class='left'>
        <h1>ABC</h1></td>
    <td class='right' width='63'>
        <img src='http://xxx/images/logo.png' /></td>
</tr>
</table><br /><br />
$name ($email) has just sent you an e-mail.
</body></html>";
$message_from="<html><body>
<table class='page-head' align='center' width='100%'>
<tr>
    <td class='left'>
        <h1>ABC</h1></td>
    <td class='right' width='63'>
        <img src='http://xxx/images/logo.png' /></td>
</tr>
</table><br /><br />
Thanks for sending the email.
</body></html>";

if ($name == "" || $email == "")
{
$error = "<font color=\"red\">Please fill in all the required fields.</font>";
} 
elseif (isset($_COOKIE['Form']))
{
$error = "You have already sent the email. Please try again later.";
}
else
{
mail($to,"A new email from: $name",$message_to,$headers);
mail($email,"Thank you for send the email",$message_from,$headers);
$success = "Emai sent successfully!";
}

$json = array('error' => $error, 'success' => $success);
print(json_encode($json));

?>

May someone give some advises on the code? Thanks a lot.

© Stack Overflow or respective owner

Related posts about php